home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / Net / Ping.php < prev    next >
PHP Script  |  2004-03-24  |  34KB  |  1,099 lines

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Martin Jansen <mj@php.net>                                  |
  17. // |          Tomas V.V.Cox <cox@idecnet.com>                             |
  18. // |          Jan Lehnardt  <jan@php.net>                                 |
  19. // |          Kai Schr÷der <k.schroeder@php.net>                          |
  20. // +----------------------------------------------------------------------+
  21. //
  22. // $Id: Ping.php,v 1.36 2004/02/08 23:17:22 jan Exp $
  23.  
  24. require_once "PEAR.php";
  25. require_once "OS/Guess.php";
  26.  
  27. define('NET_PING_FAILED_MSG',                     'execution of ping failed'        );
  28. define('NET_PING_HOST_NOT_FOUND_MSG',             'unknown host'                    );
  29. define('NET_PING_INVALID_ARGUMENTS_MSG',          'invalid argument array'          );
  30. define('NET_PING_CANT_LOCATE_PING_BINARY_MSG',    'unable to locate the ping binary');
  31. define('NET_PING_RESULT_UNSUPPORTED_BACKEND_MSG', 'Backend not Supported'           );
  32.  
  33. define('NET_PING_FAILED',                     0);
  34. define('NET_PING_HOST_NOT_FOUND',             1);
  35. define('NET_PING_INVALID_ARGUMENTS',          2);
  36. define('NET_PING_CANT_LOCATE_PING_BINARY',    3);
  37. define('NET_PING_RESULT_UNSUPPORTED_BACKEND', 4);
  38.  
  39. /**************************TODO*******************************************/
  40. /*
  41.  *
  42.  * - add Net_Ping_Result parser for:
  43.  *   + IRIX64
  44.  *   + OSF1
  45.  *   + BSD/OS
  46.  *   + OpenBSD
  47.  * - fix Net_Ping::checkHost()
  48.  * - reset result buffer
  49.  */
  50.  
  51. /**
  52. * Wrapper class for ping calls
  53. *
  54. * Usage:
  55. *
  56. * <?php
  57. *   require_once "Net/Ping.php";
  58. *   $ping = Net_Ping::factory();
  59. *   if(PEAR::isError($ping)) {
  60. *     echo $ping->getMessage();
  61. *   } else {
  62. *     $ping->setArgs(array('count' => 2));
  63. *     var_dump($ping->ping('example.com'));
  64. *   }
  65. * ?>
  66. *
  67. * @author   Jan Lehnardt <jan@php.net>
  68. * @version  $Revision: 1.36 $
  69. * @package  Net
  70. * @access   public
  71. */
  72. class Net_Ping
  73. {
  74.     /**
  75.     * Location where the ping program is stored
  76.     *
  77.     * @var string
  78.     * @access private
  79.     */
  80.     var $_ping_path = "";
  81.  
  82.     /**
  83.     * Array with the result from the ping execution
  84.     *
  85.     * @var array
  86.     * @access private
  87.     */
  88.     var $_result = array();
  89.  
  90.     /**
  91.     * OS_Guess instance
  92.     *
  93.     * @var object
  94.     * @access private
  95.     */
  96.     var $_OS_Guess = "";
  97.  
  98.     /**
  99.     * OS_Guess->getSysname result
  100.     *
  101.     * @var string
  102.     * @access private
  103.     */
  104.     var $_sysname = "";
  105.  
  106.     /**
  107.     * Ping command arguments
  108.     *
  109.     * @var array
  110.     * @access private
  111.     */
  112.     var $_args = array();
  113.  
  114.     /**
  115.     * Indicates if an empty array was given to setArgs
  116.     *
  117.     * @var boolean
  118.     * @access private
  119.     */
  120.     var $_noArgs = true;
  121.  
  122.     /**
  123.     * Contains the argument->option relation
  124.     *
  125.     * @var array
  126.     * @access private
  127.     */
  128.     var $_argRelation = array();
  129.  
  130.     /**
  131.     * Constructor for the Class
  132.     *
  133.     * @access private
  134.     */
  135.     function Net_Ping($ping_path, $sysname)
  136.     {
  137.         $this->_ping_path = $ping_path;
  138.         $this->_sysname   = $sysname;
  139.         $this->_initArgRelation();
  140.     } /* function Net_Ping() */
  141.  
  142.     /**
  143.     * Factory for Net_Ping
  144.     *
  145.     * @access public
  146.     */
  147.     function factory()
  148.     {
  149.         $ping_path = '';
  150.  
  151.         $sysname = Net_Ping::_setSystemName();
  152.  
  153.         if (($ping_path = Net_Ping::_setPingPath($sysname)) == NET_PING_CANT_LOCATE_PING_BINARY) {
  154.             return PEAR::throwError(NET_PING_CANT_LOCATE_PING_BINARY_MSG, NET_PING_CANT_LOCATE_PING_BINARY);
  155.         } else {
  156.             return new Net_Ping($ping_path, $sysname);
  157.         }
  158.     } /* function factory() */
  159.  
  160.     /** 
  161.      * Resolve the system name
  162.      *
  163.      * @access private
  164.      */
  165.     function _setSystemName()
  166.     {
  167.         $OS_Guess  = new OS_Guess;
  168.         $sysname   = $OS_Guess->getSysname();
  169.  
  170.         /* Nasty hack for Debian, as it uses a custom ping version */
  171.         if ('linux' == $sysname) {
  172.             if (file_exists('/etc/debian_version')) {
  173.                 $sysname = 'debian';
  174.             }
  175.         }
  176.  
  177.         return $sysname;
  178.         
  179.     } /* function _setSystemName */
  180.  
  181.     /**
  182.     * Set the arguments array
  183.     *
  184.     * @param array $args Hash with options
  185.     * @return mixed true or PEAR_error
  186.     * @access public
  187.     */
  188.     function setArgs($args)
  189.     {
  190.         if (!is_array($args)) {
  191.             return PEAR::throwError(NET_PING_INVALID_ARGUMENTS_MSG, NET_PING_INVALID_ARGUMENTS);
  192.         }
  193.  
  194.         $this->_setNoArgs($args);
  195.  
  196.         $this->_args = $args;
  197.  
  198.         return true;
  199.     } /* function setArgs() */
  200.  
  201.     /**
  202.     * Set the noArgs flag
  203.     *
  204.     * @param array $args Hash with options
  205.     * @return void
  206.     * @access private
  207.     */
  208.     function _setNoArgs($args)
  209.     {
  210.         if (0 == count($args)) {
  211.             $this->_noArgs = true;
  212.         } else {
  213.             $this->_noArgs = false;
  214.         }
  215.     } /* function _setNoArgs() */
  216.  
  217.     /**
  218.     * Sets the system's path to the ping binary
  219.     *
  220.     * @access private
  221.     */
  222.     function _setPingPath($sysname)
  223.     {
  224.         $status    = '';
  225.         $output    = array();
  226.         $ping_path = '';
  227.  
  228.         if ("windows" == $sysname) {
  229.             return "ping";
  230.         } else {
  231.             $ping_path = exec("which ping", $output, $status);
  232.             if (0 != $status) {
  233.                 return NET_PING_CANT_LOCATE_PING_BINARY;
  234.             } else {
  235.                 return $ping_path;
  236.             }
  237.         }
  238.     } /* function _setPingPath() */
  239.  
  240.     /**
  241.     * Creates the argument list according to platform differences
  242.     *
  243.     * @return string Argument line
  244.     * @access private
  245.     */
  246.     function _createArgList()
  247.     {
  248.         $retval     = array();
  249.  
  250.         $timeout    = "";
  251.         $iface      = "";
  252.         $ttl        = "";
  253.         $count      = "";
  254.         $quiet      = "";
  255.         $size       = "";
  256.         $seq        = "";
  257.         $deadline   = "";
  258.  
  259.         foreach($this->_args AS $option => $value) {
  260.             if(!empty($option) && isset($this->_argRelation[$this->_sysname][$option]) && NULL != $this->_argRelation[$this->_sysname][$option]) {
  261.                 ${$option} = $this->_argRelation[$this->_sysname][$option]." ".$value." ";
  262.              }
  263.         }
  264.  
  265.         switch($this->_sysname) {
  266.  
  267.         case "sunos":
  268.              if ($size || $count || $iface) {
  269.                  /* $size and $count must be _both_ defined */
  270.                  $seq = " -s ";
  271.                  if ($size == "") {
  272.                      $size = " 56 ";
  273.                  }
  274.                  if ($count == "") {
  275.                      $count = " 5 ";
  276.                  }
  277.              }
  278.              $retval['pre'] = $iface.$seq.$ttl;
  279.              $retval['post'] = $size.$count;
  280.              break;
  281.  
  282.         case "freebsd":
  283.              $retval['pre'] = $quiet.$count.$ttl.$timeout;
  284.              $retval['post'] = "";
  285.              break;
  286.  
  287.         case "darwin":
  288.              $retval['pre'] = $count.$timeout.$size;
  289.              $retval['post'] = "";
  290.              break;
  291.  
  292.         case "netbsd":
  293.              $retval['pre'] = $quiet.$count.$iface.$size.$ttl.$timeout;
  294.              $retval['post'] = "";
  295.              break;
  296.  
  297.         case "linux":
  298.              $retval['pre'] = $quiet.$deadline.$count.$ttl.$size.$timeout;
  299.              $retval['post'] = "";
  300.              break;
  301.  
  302.         case "debian":
  303.              $retval['pre'] = $quiet.$count.$ttl.$size.$timeout;
  304.              $retval['post'] = "";
  305.  
  306.              /* undo nasty debian hack*/
  307.              $this->_sysname = 'linux';
  308.              break;
  309.  
  310.         case "windows":
  311.              $retval['pre'] = $count.$ttl.$timeout;
  312.              $retval['post'] = "";
  313.              break;
  314.  
  315.         case "hpux":
  316.              $retval['pre'] = $ttl;
  317.              $retval['post'] = $size.$count;
  318.              break;
  319.  
  320.         case "aix":
  321.             $retval['pre'] = $count.$timeout.$ttl.$size;
  322.             $retval['post'] = "";
  323.             break;
  324.  
  325.         default:
  326.              $retval['pre'] = "";
  327.              $retval['post'] = "";
  328.              break;
  329.         }
  330.         return($retval);
  331.     }  /* function _createArgList() */
  332.  
  333.     /**
  334.     * Execute ping
  335.     *
  336.     * @param  string    $host   hostname
  337.     * @return mixed  String on error or array with the result
  338.     * @access public
  339.     */
  340.     function ping($host)
  341.     {
  342.         
  343.         if($this->_noArgs) {
  344.             $this->setArgs(array('count' => 3));
  345.         }
  346.  
  347.         $argList = $this->_createArgList();
  348.         $cmd = $this->_ping_path." ".$argList['pre']." ".$host." ".$argList['post'];
  349.         exec($cmd, $this->_result);
  350.  
  351.         if (!is_array($this->_result)) {
  352.             return PEAR::throwError(NET_PING_FAILED_MSG, NET_PING_FAILED);
  353.         }
  354.  
  355.         if (count($this->_result) == 0) {
  356.             return PEAR::throwError(NET_PING_HOST_NOT_FOUND_MSG, NET_PING_HOST_NOT_FOUND);
  357.         } else {
  358.             return Net_Ping_Result::factory($this->_result, $this->_sysname);
  359.         }
  360.     } /* function ping() */
  361.  
  362.     /**
  363.     * Check if a host is up by pinging it
  364.     *
  365.     * @param string $host   The host to test
  366.     * @param bool $severely If some of the packages did reach the host
  367.     *                       and severely is false the function will return true
  368.     * @return bool True on success or false otherwise
  369.     *
  370.     */
  371.     function checkHost($host, $severely = true)
  372.     {
  373.         $matches = array();
  374.         
  375.         $this->setArgs(array("count" => 10,
  376.                              "size"  => 32,
  377.                              "quiet" => null,
  378.                              "deadline" => 10
  379.                              )
  380.                        );
  381.         $res = $this->ping($host);
  382.         if (PEAR::isError($res)) {
  383.             return false;
  384.         }
  385.         if (!preg_match_all('|\d+|', $res[3], $matches) || count($matches[0]) < 3) {
  386.             ob_start();
  387.             $rep = ob_get_contents();
  388.             ob_end_clean();
  389.             trigger_error("Output format seems not to be supported, please report ".
  390.                           "the following to pear-dev@lists.php.net, including your ".
  391.                           "version of ping:\n $rep");
  392.             return false;
  393.         }
  394.         if ($matches[0][1] == 0) {
  395.             return false;
  396.         }
  397.         // [0] => transmitted, [1] => received
  398.         if ($matches[0][0] != $matches[0][1] && $severely) {
  399.             return false;
  400.         }
  401.         return true;
  402.     } /* function checkHost() */
  403.  
  404.     /**
  405.     * Output errors with PHP trigger_error(). You can silence the errors
  406.     * with prefixing a "@" sign to the function call: @Net_Ping::ping(..);
  407.     *
  408.     * @param mixed $error a PEAR error or a string with the error message
  409.     * @return bool false
  410.     * @access private
  411.     * @author Kai Schr÷der <k.schroeder@php.net>
  412.     */
  413.     function _raiseError($error)
  414.     {
  415.         if (PEAR::isError($error)) {
  416.             $error = $error->getMessage();
  417.         }
  418.         trigger_error($error, E_USER_WARNING);
  419.         return false;
  420.     }  /* function _raiseError() */
  421.  
  422.     /**
  423.     * Creates the argument list according to platform differences
  424.     *
  425.     * @return string Argument line
  426.     * @access private
  427.     */
  428.     function _initArgRelation()
  429.     {
  430.         $this->_argRelation["sunos"] = array(
  431.                                              "timeout"   => NULL,
  432.                                              "ttl"       => "-t",
  433.                                              "count"     => " ",
  434.                                              "quiet"     => "-q",
  435.                                              "size"      => " ",
  436.                                              "iface"     => "-i"
  437.                                              );
  438.  
  439.         $this->_argRelation["freebsd"] = array (
  440.                                                 "timeout"   => "-t",
  441.                                                 "ttl"       => "-m",
  442.                                                 "count"     => "-c",
  443.                                                 "quiet"     => "-q",
  444.                                                 "size"      => NULL,
  445.                                                 "iface"     => NULL
  446.                                                 );
  447.  
  448.         $this->_argRelation["netbsd"] = array (
  449.                                                "timeout"   => "-w",
  450.                                                "iface"     => "-I",
  451.                                                "ttl"       => "-T",
  452.                                                "count"     => "-c",
  453.                                                "quiet"     => "-q",
  454.                                                "size"      => "-s"
  455.                                                );
  456.  
  457.         $this->_argRelation["openbsd"] = array (
  458.                                                 "timeout"   => "-w",
  459.                                                 "iface"     => "-I",
  460.                                                 "ttl"       => "-t",
  461.                                                 "count"     => "-c",
  462.                                                 "quiet"     => "-q",
  463.                                                 "size"      => "-s"
  464.                                                 );
  465.  
  466.         $this->_argRelation["darwin"] = array (
  467.                                                "timeout"   => "-t",
  468.                                                "iface"     => NULL,
  469.                                                "ttl"       => NULL,
  470.                                                "count"     => "-c",
  471.                                                "quiet"     => "-q",
  472.                                                "size"      => NULL
  473.                                                );
  474.  
  475.         $this->_argRelation["linux"] = array (
  476.                                               "timeout"   => "-t",
  477.                                               "iface"     => NULL,
  478.                                               "ttl"       => "-m",
  479.                                               "count"     => "-c",
  480.                                               "quiet"     => "-q",
  481.                                               "size"      => "-s",
  482.                                               "deadline"  => "-w"
  483.                                               );
  484.  
  485.         $this->_argRelation["debian"] = array (
  486.                                               "timeout"   => "-t",
  487.                                               "iface"     => NULL,
  488.                                               "ttl"       => "-m",
  489.                                               "count"     => "-c",
  490.                                               "quiet"     => "-q",
  491.                                               "size"      => "-s",
  492.                                               );
  493.  
  494.         $this->_argRelation["windows"] = array (
  495.                                                 "timeout"   => "-w",
  496.                                                 "iface"     => NULL,
  497.                                                 "ttl"       => "-i",
  498.                                                 "count"     => "-n",
  499.                                                 "quiet"     => NULL,
  500.                                                 "size"      => "-l"
  501.                                                  );
  502.  
  503.         $this->_argRelation["hpux"] = array (
  504.                                              "timeout"   => NULL,
  505.                                              "iface"     => NULL,
  506.                                              "ttl"       => "-t",
  507.                                              "count"     => "-n",
  508.                                              "quiet"     => NULL,
  509.                                              "size"      => " "
  510.                                              );
  511.  
  512.         $this->_argRelation["aix"] = array (
  513.                                             "timeout"   => "-i",
  514.                                             "iface"     => NULL,
  515.                                             "ttl"       => "-T",
  516.                                             "count"     => "-c",
  517.                                             "quiet"     => NULL,
  518.                                             "size"      => "-s"
  519.                                             );
  520.     }  /* function _initArgRelation() */
  521. } /* class Net_Ping */
  522.  
  523. /**
  524. * Container class for Net_Ping results
  525. *
  526. * @author   Jan Lehnardt <jan@php.net>
  527. * @version  $Revision: 1.36 $
  528. * @package  Net
  529. * @access   private
  530. */
  531. class Net_Ping_Result
  532. {
  533.     /**
  534.     * ICMP sequence number and associated time in ms
  535.     *
  536.     * @var array
  537.     * @access private
  538.     */
  539.     var $_icmp_sequence = array(); /* array($sequence_number => $time ) */
  540.  
  541.     /**
  542.     * The target's IP Address
  543.     *
  544.     * @var string
  545.     * @access private
  546.     */
  547.     var $_target_ip;
  548.  
  549.     /**
  550.     * Number of bytes that are sent with each ICMP request
  551.     *
  552.     * @var int
  553.     * @access private
  554.     */
  555.     var $_bytes_per_request;
  556.  
  557.     /**
  558.     * The total number of bytes that are sent with all ICMP requests
  559.     *
  560.     * @var int
  561.     * @access private
  562.     */
  563.     var $_bytes_total;
  564.  
  565.     /**
  566.     * The ICMP request's TTL
  567.     *
  568.     * @var int
  569.     * @access private
  570.     */
  571.     var $_ttl;
  572.  
  573.     /**
  574.     * The raw Net_Ping::result
  575.     *
  576.     * @var array
  577.     * @access private
  578.     */
  579.     var $_raw_data = array();
  580.  
  581.     /**
  582.     * The Net_Ping::_sysname
  583.     *
  584.     * @var int
  585.     * @access private
  586.     */
  587.     var $_sysname;
  588.  
  589.     /**
  590.     * Statistical information about the ping
  591.     *
  592.     * @var int
  593.     * @access private
  594.     */
  595.     var $_round_trip = array(); /* array('min' => xxx, 'avg' => yyy, 'max' => zzz) */
  596.  
  597.  
  598.     /**
  599.     * Constructor for the Class
  600.     *
  601.     * @access private
  602.     */
  603.     function Net_Ping_Result($result, $sysname)
  604.     {
  605.         $this->_raw_data = $result;
  606.         $this->_sysname  = $sysname;
  607.  
  608.         $this->_parseResult();
  609.     } /* function Net_Ping_Result() */
  610.  
  611.     /**
  612.     * Factory for Net_Ping_Result
  613.     *
  614.     * @access public
  615.     * @param array $result Net_Ping result
  616.     * @param string $sysname OS_Guess::sysname
  617.     */
  618.     function factory($result, $sysname)
  619.     {
  620.         if (!Net_Ping_Result::_prepareParseResult($sysname)) {
  621.             return PEAR::throwError(NET_PING_RESULT_UNSUPPORTED_BACKEND_MSG, NET_PING_RESULT_UNSUPPORTED_BACKEND);
  622.         } else {
  623.             return new Net_Ping_Result($result, $sysname);
  624.         }
  625.     }  /* function factory() */
  626.  
  627.     /**
  628.     * Preparation method for _parseResult
  629.     *
  630.     * @access private
  631.     * @param string $sysname OS_Guess::sysname
  632.     * $return bool
  633.     */
  634.     function _prepareParseResult($sysname)
  635.     {
  636.         $parse_methods = array_values(get_class_methods('Net_Ping_Result'));
  637.  
  638.         return in_array('_parseresult'.$sysname, $parse_methods);
  639.     } /* function _prepareParseResult() */
  640.  
  641.     /**
  642.     * Delegates the parsing routine according to $this->_sysname
  643.     *
  644.     * @access private
  645.     */
  646.     function _parseResult()
  647.     {
  648.         call_user_func(array(&$this, '_parseResult'.$this->_sysname));
  649.     } /* function _parseResult() */
  650.  
  651.     /**
  652.     * Parses the output of Linux' ping command
  653.     *
  654.     * @access private
  655.     * @see _parseResultlinux
  656.     */
  657.     function _parseResultlinux()
  658.     {
  659.         $raw_data_len   = count($this->_raw_data);
  660.         $icmp_seq_count = $raw_data_len - 4;
  661.  
  662.         /* loop from second elment to the fifths last */
  663.         for($idx = 1; $idx < $icmp_seq_count; $idx++) {
  664.                 $parts = explode(' ', $this->_raw_data[$idx]);
  665.                 $this->_icmp_sequence[substr(@$parts[4], 9, strlen(@$parts[4]))] = substr(@$parts[6], 5, strlen(@$parts[6]));
  666.             }
  667.         $this->_bytes_per_request = $parts[0];
  668.         $this->_bytes_total       = (int)$parts[0] * $icmp_seq_count;
  669.         $this->_target_ip         = substr($parts[3], 0, -1);
  670.         $this->_ttl               = substr($parts[5], 4, strlen($parts[3]));
  671.  
  672.         $stats = explode(',', $this->_raw_data[$raw_data_len - 2]);
  673.         $transmitted = explode(' ', $stats[0]);
  674.         $this->_transmitted = $transmitted[0];
  675.  
  676.         $received = explode(' ', $stats[1]);
  677.         $this->_received = $received[1];
  678.  
  679.         $loss = explode(' ', $stats[2]);
  680.         $this->_loss = (int)$loss[1];
  681.  
  682.         $round_trip = explode('/', str_replace('=', '/', substr($this->_raw_data[$raw_data_len - 1], 0, -3)));
  683.  
  684.         /* if mdev field exists, shift input one unit left */
  685.         if (strpos($this->_raw_data[$raw_data_len - 1], 'mdev')) {
  686.             /* do not forget the rtt field */
  687.             $this->_round_trip['min']    = ltrim($round_trip[5]);
  688.             $this->_round_trip['avg']    = $round_trip[6];
  689.             $this->_round_trip['max']    = $round_trip[7];
  690.         } else {
  691.             $this->_round_trip['min']    = ltrim($round_trip[4]);
  692.             $this->_round_trip['avg']    = $round_trip[5];
  693.             $this->_round_trip['max']    = $round_trip[6];
  694.         }
  695.     } /* function _parseResultlinux() */
  696.  
  697.     /**
  698.     * Parses the output of NetBSD's ping command
  699.     *
  700.     * @access private
  701.     * @see _parseResultfreebsd
  702.     */
  703.     function _parseResultnetbsd()
  704.     {
  705.         $this->_parseResultfreebsd();
  706.     } /* function _parseResultnetbsd() */
  707.   
  708.     /**
  709.     * Parses the output of Darwin's ping command
  710.     *
  711.     * @access private
  712.     */
  713.     function _parseResultdarwin()
  714.     {
  715.         $raw_data_len   = count($this->_raw_data);
  716.         $icmp_seq_count = $raw_data_len - 5;
  717.  
  718.         /* loop from second elment to the fifths last */
  719.         for($idx = 1; $idx < $icmp_seq_count; $idx++) {
  720.             $parts = explode(' ', $this->_raw_data[$idx]);
  721.             $this->_icmp_sequence[substr($parts[4], 9, strlen($parts[4]))] = substr($parts[6], 5, strlen($parts[6]));
  722.         }
  723.  
  724.         $this->_bytes_per_request = (int)$parts[0];
  725.         $this->_bytes_total       = (int)($this->_bytes_per_request * $icmp_seq_count);
  726.         $this->_target_ip         = substr($parts[3], 0, -1);
  727.         $this->_ttl               = (int)substr($parts[5], 4, strlen($parts[3]));
  728.  
  729.         $stats = explode(',', $this->_raw_data[$raw_data_len - 2]);
  730.         $transmitted = explode(' ', $stats[0]);
  731.         $this->_transmitted = (int)$transmitted[0];
  732.  
  733.         $received = explode(' ', $stats[1]);
  734.         $this->_received = (int)$received[1];
  735.  
  736.         $loss = explode(' ', $stats[2]);
  737.         $this->_loss = (int)$loss[1];
  738.  
  739.         $round_trip = explode('/', str_replace('=', '/', substr($this->_raw_data[$raw_data_len - 1], 0, -3)));
  740.  
  741.         $this->_round_trip['min']    = (float)ltrim($round_trip[3]);
  742.         $this->_round_trip['avg']    = (float)$round_trip[4];
  743.         $this->_round_trip['max']    = (float)$round_trip[5];
  744.         $this->_round_trip['stddev'] = NULL; /* no stddev */
  745.     } /* function _parseResultdarwin() */
  746.  
  747.     /**
  748.     * Parses the output of HP-UX' ping command
  749.     *
  750.     * @access private
  751.     */   
  752.     function _parseResulthpux()
  753.     {
  754.         $parts          = array();
  755.         $raw_data_len   = count($this->_raw_data);
  756.         $icmp_seq_count = $raw_data_len - 5;
  757.  
  758.         /* loop from second elment to the fifths last */
  759.         for($idx = 1; $idx <= $icmp_seq_count; $idx++) {
  760.             $parts = explode(' ', $this->_raw_data[$idx]);
  761.             $this->_icmp_sequence[(int)substr($parts[4], 9, strlen($parts[4]))] = (int)substr($parts[5], 5, strlen($parts[5]));
  762.         }
  763.         $this->_bytes_per_request = (int)$parts[0];
  764.         $this->_bytes_total       = (int)($parts[0] * $icmp_seq_count);
  765.         $this->_target_ip         = NULL; /* no target ip */
  766.         $this->_ttl               = NULL; /* no ttl */
  767.  
  768.         $stats = explode(',', $this->_raw_data[$raw_data_len - 2]);
  769.         $transmitted = explode(' ', $stats[0]);
  770.         $this->_transmitted = (int)$transmitted[0];
  771.  
  772.         $received = explode(' ', $stats[1]);
  773.         $this->_received = (int)$received[1];
  774.  
  775.         $loss = explode(' ', $stats[2]);
  776.         $this->_loss = (int)$loss[1];
  777.  
  778.         $round_trip = explode('/', str_replace('=', '/',$this->_raw_data[$raw_data_len - 1]));
  779.  
  780.         $this->_round_trip['min']    = (int)ltrim($round_trip[3]);
  781.         $this->_round_trip['avg']    = (int)$round_trip[4];
  782.         $this->_round_trip['max']    = (int)$round_trip[5];
  783.         $this->_round_trip['stddev'] = NULL; /* no stddev */
  784.     } /* function _parseResulthpux() */
  785.  
  786.     /**
  787.     * Parses the output of AIX' ping command
  788.     *
  789.     * @access private
  790.     */   
  791.     function _parseResultaix()
  792.     {
  793.         $parts          = array();
  794.         $raw_data_len   = count($this->_raw_data);
  795.         $icmp_seq_count = $raw_data_len - 5;
  796.  
  797.         /* loop from second elment to the fifths last */
  798.         for($idx = 1; $idx <= $icmp_seq_count; $idx++) {
  799.             $parts = explode(' ', $this->_raw_data[$idx]);
  800.             $this->_icmp_sequence[(int)substr($parts[4], 9, strlen($parts[4]))] = (int)substr($parts[6], 5, strlen($parts[6]));
  801.         }
  802.         $this->_bytes_per_request = (int)$parts[0];
  803.         $this->_bytes_total       = (int)($parts[0] * $icmp_seq_count);
  804.         $this->_target_ip         = substr($parts[3], 0, -1);
  805.         $this->_ttl               = (int)substr($parts[5], 4, strlen($parts[3]));
  806.  
  807.         $stats = explode(',', $this->_raw_data[$raw_data_len - 2]);
  808.         $transmitted = explode(' ', $stats[0]);
  809.         $this->_transmitted = (int)$transmitted[0];
  810.  
  811.         $received = explode(' ', $stats[1]);
  812.         $this->_received = (int)$received[1];
  813.  
  814.         $loss = explode(' ', $stats[2]);
  815.         $this->_loss = (int)$loss[1];
  816.  
  817.         $round_trip = explode('/', str_replace('=', '/',$this->_raw_data[$raw_data_len - 1]));
  818.  
  819.         $this->_round_trip['min']    = (int)ltrim($round_trip[3]);
  820.         $this->_round_trip['avg']    = (int)$round_trip[4];
  821.         $this->_round_trip['max']    = (int)$round_trip[5];
  822.         $this->_round_trip['stddev'] = NULL; /* no stddev */
  823.     } /* function _parseResultaix() */
  824.  
  825.     /**
  826.     * Parses the output of FreeBSD's ping command
  827.     *
  828.     * @access private
  829.     */
  830.     function _parseResultfreebsd()
  831.     {
  832.         $raw_data_len   = count($this->_raw_data);
  833.         $icmp_seq_count = $raw_data_len - 5;
  834.  
  835.         /* loop from second elment to the fifths last */
  836.         for($idx = 1; $idx < $icmp_seq_count; $idx++) {
  837.            $parts = explode(' ', $this->_raw_data[$idx]);
  838.            $this->_icmp_sequence[substr($parts[4], 9, strlen($parts[4]))] = substr($parts[6], 5, strlen($parts[6]));
  839.         }
  840.  
  841.         $this->_bytes_per_request = (int)$parts[0];
  842.         $this->_bytes_total       = (int)($parts[0] * $icmp_seq_count);
  843.         $this->_target_ip         = substr($parts[3], 0, -1);
  844.         $this->_ttl               = (int)substr($parts[5], 4, strlen($parts[3]));
  845.  
  846.         $stats = explode(',', $this->_raw_data[$raw_data_len - 2]);
  847.         $transmitted = explode(' ', $stats[0]);
  848.         $this->_transmitted = (int)$transmitted[0];
  849.  
  850.         $received = explode(' ', $stats[1]);
  851.         $this->_received = (int)$received[1];
  852.  
  853.         $loss = explode(' ', $stats[2]);
  854.         $this->_loss = (int)$loss[1];
  855.  
  856.         $round_trip = explode('/', str_replace('=', '/', substr($this->_raw_data[$raw_data_len - 1], 0, -3)));
  857.  
  858.         $this->_round_trip['min']    = (float)ltrim($round_trip[4]);
  859.         $this->_round_trip['avg']    = (float)$round_trip[5];
  860.         $this->_round_trip['max']    = (float)$round_trip[6];
  861.         $this->_round_trip['stddev'] = (float)$round_trip[7];
  862.     } /* function _parseResultfreebsd() */
  863.  
  864.     /**
  865.     * Parses the output of Windows' ping command
  866.     *
  867.     * @author Kai Schr÷der <k.schroeder@php.net>
  868.     * @access private
  869.     */
  870.     function _parseResultwindows()
  871.     {
  872.         $raw_data_len   = count($this->_raw_data);
  873.         $icmp_seq_count = $raw_data_len - 8;
  874.  
  875.         /* loop from fourth elment to the sixths last */
  876.         for($idx = 1; $idx <= $icmp_seq_count; $idx++) {
  877.             $parts = explode(' ', $this->_raw_data[$idx + 2]);
  878.             $this->_icmp_sequence[$idx - 1] = (int)substr(end(split('=', $parts[4])), 0, -2);
  879.  
  880.             $ttl = (int)substr($parts[5], 4, strlen($parts[3]));
  881.             if ($ttl > 0 && $this->_ttl == 0) {
  882.                 $this->_ttl = $ttl;
  883.             }
  884.         }
  885.  
  886.        
  887.         $parts = explode(' ', $this->_raw_data[1]);
  888.         $this->_bytes_per_request = (int)$parts[4];
  889.         $this->_bytes_total       = $this->_bytes_per_request * $icmp_seq_count;
  890.         $this->_target_ip         = substr(trim($parts[2]), 1, -1);
  891.  
  892.         $stats = explode(',', $this->_raw_data[$raw_data_len - 3]);
  893.         $transmitted = explode('=', $stats[0]);
  894.         $this->_transmitted = (int)$transmitted[1];
  895.  
  896.         $received = explode('=', $stats[1]);
  897.         $this->_received = (int)$received[1];
  898.  
  899.         $loss = explode('=', $stats[2]);
  900.         $this->_loss = (int)$loss[1];
  901.  
  902.         $round_trip = explode(',', str_replace('=', ',', $this->_raw_data[$raw_data_len - 1]));
  903.         $this->_round_trip['min'] = (int)substr(trim($round_trip[1]), 0, -2);
  904.         $this->_round_trip['avg'] = (int)substr(trim($round_trip[3]), 0, -2);
  905.         $this->_round_trip['max'] = (int)substr(trim($round_trip[5]), 0, -2);
  906.     } /* function _parseResultwindows() */
  907.  
  908.     /**
  909.     * Returns a Ping_Result property
  910.     *
  911.     * @param string $name property name
  912.     * @return mixed property value
  913.     * @access public
  914.     */
  915.     function getValue($name)
  916.     {
  917.         return isset($this->$name)?$this->$name:'';
  918.     } /* function getValue() */
  919.  
  920.     /**
  921.     * Accessor for $this->_target_ip;
  922.     *
  923.     * @return string IP address
  924.     * @access public
  925.     * @see Ping_Result::_target_ip
  926.     */
  927.     function getTargetIp()
  928.     {
  929.         return $this->_target_ip;
  930.     } /* function getTargetIp() */
  931.  
  932.     /**
  933.     * Accessor for $this->_icmp_sequence;
  934.     *
  935.     * @return array ICMP sequence
  936.     * @access private
  937.     * @see Ping_Result::_icmp_sequence
  938.     */
  939.     function getICMPSequence()
  940.     {
  941.         return $this->_icmp_sequence;
  942.     } /* function getICMPSequencs() */
  943.  
  944.     /**
  945.     * Accessor for $this->_bytes_per_request;
  946.     *
  947.     * @return int bytes per request
  948.     * @access private
  949.     * @see Ping_Result::_bytes_per_request
  950.     */
  951.     function getBytesPerRequest()
  952.     {
  953.         return $this->_bytes_per_request;
  954.     } /* function getBytesPerRequest() */
  955.  
  956.     /**
  957.     * Accessor for $this->_bytes_total;
  958.     *
  959.     * @return int total bytes
  960.     * @access private
  961.     * @see Ping_Result::_bytes_total
  962.     */
  963.     function getBytesTotal()
  964.     {
  965.         return $this->_bytes_total;
  966.     } /* function getBytesTotal() */
  967.  
  968.     /**
  969.     * Accessor for $this->_ttl;
  970.     *
  971.     * @return int TTL
  972.     * @access private
  973.     * @see Ping_Result::_ttl
  974.     */
  975.     function getTTL()
  976.     {
  977.         return $this->_ttl;
  978.     } /* function getTTL() */
  979.  
  980.     /**
  981.     * Accessor for $this->_raw_data;
  982.     *
  983.     * @return array raw data
  984.     * @access private
  985.     * @see Ping_Result::_raw_data
  986.     */
  987.     function getRawData()
  988.     {
  989.         return $this->_raw_data;
  990.     } /* function getRawData() */
  991.  
  992.     /**
  993.     * Accessor for $this->_sysname;
  994.     *
  995.     * @return string OS_Guess::sysname
  996.     * @access private
  997.     * @see Ping_Result::_sysname
  998.     */
  999.     function getSystemName()
  1000.     {
  1001.         return $this->_sysname;
  1002.     } /* function getSystemName() */
  1003.  
  1004.     /**
  1005.     * Accessor for $this->_round_trip;
  1006.     *
  1007.     * @return array statistical information
  1008.     * @access private
  1009.     * @see Ping_Result::_round_trip
  1010.     */
  1011.     function getRoundTrip()
  1012.     {
  1013.         return $this->_round_trip;
  1014.     } /* function getRoundTrip() */
  1015.  
  1016.     /**
  1017.     * Accessor for $this->_round_trip['min'];
  1018.     *
  1019.     * @return array statistical information
  1020.     * @access private
  1021.     * @see Ping_Result::_round_trip
  1022.     */
  1023.     function getMin()
  1024.     {
  1025.         return $this->_round_trip['min'];
  1026.     } /* function getMin() */
  1027.  
  1028.     /**
  1029.     * Accessor for $this->_round_trip['max'];
  1030.     *
  1031.     * @return array statistical information
  1032.     * @access private
  1033.     * @see Ping_Result::_round_trip
  1034.     */
  1035.     function getMax()
  1036.     {
  1037.         return $this->_round_trip['max'];
  1038.     } /* function getMax() */
  1039.  
  1040.     /**
  1041.     * Accessor for $this->_round_trip['stddev'];
  1042.     *
  1043.     * @return array statistical information
  1044.     * @access private
  1045.     * @see Ping_Result::_round_trip
  1046.     */
  1047.     function getStddev()
  1048.     {
  1049.         return $this->_round_trip['stddev'];
  1050.     } /* function getStddev() */
  1051.  
  1052.     /**
  1053.     * Accessor for $this->_round_tripp['avg'];
  1054.     *
  1055.     * @return array statistical information
  1056.     * @access private
  1057.     * @see Ping_Result::_round_trip
  1058.     */
  1059.     function getAvg()
  1060.     {
  1061.         return $this->_round_trip['avg'];
  1062.     } /* function getAvg() */
  1063.  
  1064.     /**
  1065.     * Accessor for $this->_transmitted;
  1066.     *
  1067.     * @return array statistical information
  1068.     * @access private
  1069.     */
  1070.     function getTransmitted()
  1071.     {
  1072.         return $this->_transmitted;
  1073.     } /* function getTransmitted() */
  1074.  
  1075.     /**
  1076.     * Accessor for $this->_received;
  1077.     *
  1078.     * @return array statistical information
  1079.     * @access private
  1080.     */
  1081.     function getReceived()
  1082.     {
  1083.         return $this->_received;
  1084.     } /* function getReceived() */
  1085.  
  1086.     /**
  1087.     * Accessor for $this->_loss;
  1088.     *
  1089.     * @return array statistical information
  1090.     * @access private
  1091.     */
  1092.     function getLoss()
  1093.     {
  1094.         return $this->_loss;
  1095.     } /* function getLoss() */
  1096.  
  1097. } /* class Net_Ping_Result */
  1098. ?>
  1099.